package com.cloud.music.config.proxy;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.cglib.proxy.Proxy;


public class MusicServiceFactoryBean<T> implements FactoryBean<T> {

    private Class<T> musicServiceInterface;

    public MusicServiceFactoryBean() {
    }

    public MusicServiceFactoryBean(Class<T> musicServiceInterface) {
        this.musicServiceInterface = musicServiceInterface;
    }

    /**
     * bean实例化对象,指向代理类即可
     */
    @Override
    public T getObject() throws Exception {
        // 返回MusicServiceProxy代理对象
        return (T) Proxy.newProxyInstance(musicServiceInterface.getClassLoader(),
                new Class[]{musicServiceInterface},
                new MusicServiceProxy<>(musicServiceInterface));
    }

    /**
     * bean对象类型
     */
    @Override
    public Class<T> getObjectType() {
        return this.musicServiceInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

